<?php
declare(strict_types=1);
/*
暂收单模块
*/
namespace Modules\Purchasing\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Purchasing\Http\Repositories\ProvisionalReget;
use Illuminate\Http\Request;
use Modules\Purchasing\Models\Provisional;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Modules\Purchasing\Models\Purchaseorder;
use Modules\Purchasing\Models\Purchaseorderschedule;
use Modules\Purchasing\Models\Barcodeprinting;
use Modules\Purchasing\Models\Supplier;
use Modules\Purchasing\Models\Supplierschedule;
use Modules\Purchasing\Models\Provisionalschedule;
use Illuminate\Support\Facades\DB;
use Modules\Purchasing\Models\Inspectionlist;
use Modules\Purchasing\Models\Inspectionlisttable;


class ProvisionalreceiptController extends Controller
{
    public function __construct(
        protected readonly Provisional $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
    
         // $data = $this->model->getList();
         $data = ProvisionalReget::list($request->all());
         $kingDeeService = new warehouseService();
         $datauser = $kingDeeService->uerdata();
         foreach($data as $k=>$v){
             $supplierOrder = Provisionalschedule::select('supplier.supplierOrder','provisionalschedule.purchaseorder_id')
             ->join('supplier as supplier', 'supplier.id', '=', 'provisionalschedule.supplier_id')
             ->where('provisionalschedule.provisional_id',$v['id'])
             ->get()->toArray();
            
             if(!empty($supplierOrder)){
                  $fbillno = '';
                  $purchaseorder_id=[];
                 foreach ($supplierOrder as $ks=>$vs){
                      $purchaseorder_id[] = $vs['purchaseorder_id'];
                 }
                 $purchaseorder_id = array_unique($purchaseorder_id);
                 
                 $purchaseorder = purchaseorder::select('fbillno','id','fpurchaseorgid_number','fpurchaseorgid_name')
                     ->whereIn('id',$purchaseorder_id)
                     ->get()->toArray();
                  
                 if(!empty($purchaseorder)){
                    foreach ($purchaseorder as $kk=>$vv){
                        $fbillno .= $vv['fbillno'].'、';       
                    }
                   
                 }
 
                 $fmaterialidName = rtrim($fbillno,'、');
                 $explode = explode('、',$fmaterialidName);
                 $explode = array_unique($explode);
                 $fbillnodata='';
                 foreach ($explode as $k1=>$v1){
                 $res = mb_convert_encoding($v1, 'UTF-8', 'UTF-8'); 
                 $fbillnodata .=$res .'、';
                 }
                 $data[$k]['fbillno'] = rtrim($fbillnodata,'、');
                 $data[$k]['supplierOrder']  = $supplierOrder[0]['supplierOrder'];
             }else{
                  $data[$k]['supplierOrder']  = '';
                  $data[$k]['fbillno'] = '';                  
             }     
             $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
             $data[$k]['modifier'] = isset($datauser[$v['modifier']])?$datauser[$v['modifier']]['username']:'';
             switch ($v['state']){
                 case 0:
                     $data[$k]['description'] = '待审核'; 
                 break;
                 case 1:
                     $data[$k]['description'] = '已审核'; 
                 break;
             }
         }
         return $data;
    }


    
    /**
     * @param Request $request
     * @return mixed
     */
    // public function Notinspected(Request $request): mixed
    // {
    //     $data = $request->all();
    //     $data = ProvisionalReget::list($data);
    //     // foreach($data as $k=>$){}

    //     $data = $this->model->w->getList();
    //     // $data = ProvisionalReget::list($request->all());
    //     $kingDeeService = new warehouseService();
    //     $datauser = $kingDeeService->uerdata();
    //     foreach($data as $k=>$v){
    //         $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
    //         $data[$k]['modifier'] = isset($datauser[$v['modifier']])?$datauser[$v['modifier']]['username']:'';
    //         switch ($v['state']){
    //             case 0:
    //                 $data[$k]['description'] = '待审核'; 
    //             break;
    //             case 1:
    //                 $data[$k]['description'] = '已审核'; 
    //             break;
    //         }
    //     }
    //     return $data;
    // }


    /**数据添加接口
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        
        $data = $request->all();
        $data['creator_id'] =$this->getLoginUserId();//当前登录用户名称
        return ProvisionalReget::create($data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id,Request $request)
    {
        
        $resData = [];
        $data = $request->all();

        $provisional = provisional::select('ordernumber','remarks','state')->where('id',$id)->first();

        $datares = Provisionalschedule::query()
        ->where('provisional_id',$id);
        if(!isset($data['provisional_id'])){
                $datares = $datares->select('provisional_id','supplierschedule_id',DB::raw('SUM(quantityreceived) as quantityreceived'),'storagelocation','inspectionlisttable_id','purchaseorderschedule_id');
                $datares = $datares->groupby(['supplierschedule_id','provisional_id','storagelocation','inspectionlisttable_id','purchaseorderschedule_id']);
        }
        $datares = $datares->get()->toArray();
        $dataRes = ProvisionalReget::showApi($datares);


        
        if(isset($data['provisional_id'])){
            foreach($dataRes as $k=>$v){

                $Inspectionlist = Inspectionlist::select('inspectionresult')->where('id',$v['inspectionlist_id'])->first();
                if(!empty($Inspectionlist)){
                    if($Inspectionlist['inspectionresult']==0){
                        $dataRes[$k]['inspectionresult'] = '合格';
                    }else if($Inspectionlist['inspectionresult']==1){
                        $dataRes[$k]['inspectionresult'] = '不合格';
                    }
                }else{
                    $dataRes[$k]['inspectionresult'] = '未检验';
                }
                
            }
        }

        $Inspectionlisttable = Inspectionlisttable::select('inspectionnumber','state')->where('id',$dataRes[0]['inspectionlisttable_id'])->first();
        $resData['dataRes'] = $dataRes;
        $resData['provisional'] = $provisional;
        $resData['inspectionlisttable'] = $Inspectionlisttable;
        return $data=['code'=>1000,'message'=>'审核成功','data'=>$resData];
      
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id,Request $request)
    {   
        $data = $request->all();
        $data['modifier'] =$this->getLoginUserId();//当前登录用户名称
        return ProvisionalReget::createUpdate($id,$data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return ProvisionalReget::delete($id);
    }

   

     /*暂收单审核接口
     * @param $id
     * @return mixed
     */
    public function interfaceApi($id)
    {
        return ProvisionalReget::auditupdate($id);
    }

    


}
